home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / os2 / remin301.zip / REMIN300.ZIP / INIT.C < prev    next >
C/C++ Source or Header  |  1992-11-10  |  10KB  |  330 lines

  1. /***************************************************************/
  2. /*                                                             */
  3. /*  INIT.C                                                     */
  4. /*                                                             */
  5. /*  Initialize remind; perform certain tasks between           */
  6. /*  iterations in calendar mode; do certain checks after end   */
  7. /*  in normal mode.                                            */
  8. /*                                                             */
  9. /*  This file is part of REMIND.                               */
  10. /*  Copyright (C) 1992 by David F. Skoll.                      */
  11. /*                                                             */
  12. /***************************************************************/
  13. #include "config.h"
  14. #include <stdio.h>
  15. #ifdef HAVE_STDLIB_H
  16. #include <stdlib.h>
  17. #endif
  18. #ifdef HAVE_MALLOC_H
  19. #include <malloc.h>
  20. #endif
  21. #include <ctype.h>
  22. #include "types.h"
  23. #include "protos.h"
  24. #include "expr.h"
  25. #include "globals.h"
  26. #include "err.h"
  27. #include "version.h"
  28.  
  29. /***************************************************************
  30.  *
  31.  *  Command line options recognized:
  32.  *
  33.  *  -n       = Output next trigger date of each reminder in
  34.  *             simple calendar format.
  35.  *  -r       = Disallow RUN mode
  36.  *  -c[n]    = Produce a calendar for n months (default = 1)
  37.  *  -w[n]    = Specify output device width (default = 80)
  38.  *  -s[n]    = Produce calendar in "simple calendar" format
  39.  *  -v       = Verbose mode
  40.  *  -o       = Ignore ONCE directives
  41.  *  -a       = Don't issue timed reminders which will be queued
  42.  *  -q       = Don't queue timed reminders
  43.  *  -t       = Trigger all reminders (infinite delta)
  44.  *  -h       = Hush mode
  45.  *  -f       = Do not fork
  46.  *  -dchars  = Debugging mode:  Chars are:
  47.  *             e = Echo input lines
  48.  *             x = Display expression evaluation
  49.  *             t = Display trigger dates
  50.  *             v = Dump variables at end
  51.  *             l = Display entire line in error messages
  52.  *  -e       = Send messages normally sent to stderr to stdout instead
  53.  *  -z[n]    = Daemon mode waking up every n (def 5) minutes.
  54.  *  -bn      = Time format for cal (0, 1, or 2)
  55.  *  -xn      = Max. number of iterations for SATISFY
  56.  *
  57.  **************************************************************/
  58.  
  59. /* For parsing an integer */
  60. #define PARSENUM(var, s)   \
  61.    var = 0;                \
  62.    while (isdigit(*(s))) { \
  63.       var *= 10;           \
  64.       var += *(s) - '0';   \
  65.       s++;                 \
  66.    }
  67.  
  68. static char *BadDate = "Illegal date on command line\n";
  69.  
  70. /***************************************************************/
  71. /*                                                             */
  72. /*  InitRemind                                                 */
  73. /*                                                             */
  74. /*  Initialize the system - called only once at beginning!     */
  75. /*                                                             */
  76. /***************************************************************/
  77. #ifdef HAVE_PROTOS
  78. PUBLIC void InitRemind(int argc, char *argv[])
  79. #else
  80. void InitRemind(argc, argv)
  81. int argc;
  82. char *argv[];
  83. #endif
  84. {
  85.    char *arg;
  86.    int i;
  87.    int y, m, d;
  88.    Token tok;
  89.  
  90.    y = NO_YR;
  91.    m = NO_MON;
  92.    d = NO_DAY;
  93.  
  94.    RealToday = SystemDate(&CurYear, &CurMon, &CurDay);
  95.    if (RealToday < 0) {
  96.       fprintf(ErrFp, "Illegal system date: Year is less than %d\n", BASE);
  97.       exit(1);
  98.    }
  99.    JulianToday = RealToday;
  100.    FirstYear = -1;
  101.    FromJulian(JulianToday, &CurYear, &CurMon, &CurDay);
  102.  
  103.    /* Parse the command-line options */
  104.    i = 1;
  105.    while (i < argc) {
  106.       arg = argv[i];
  107.       if (*arg != '-') break;  /* Exit the loop if it's not an option */
  108.       i++;
  109.       arg++;
  110.  
  111.       while (*arg) {
  112.          switch(*arg++) {
  113.         case 'n':
  114.         case 'N':
  115.            NextMode = 1;
  116. #ifdef HAVE_QUEUED
  117.            DontQueue = 1;
  118. #endif
  119.            break;
  120.  
  121.         case 'r':
  122.         case 'R':
  123.            RunDisabled = 1;
  124.            break;
  125.  
  126.         case 'o':
  127.         case 'O':
  128.            IgnoreOnce = 1;
  129.            break;
  130.  
  131.         case 't':
  132.         case 'T':
  133.            InfiniteDelta = 1;
  134.            break;
  135.  
  136.         case 'e':
  137.         case 'E':
  138.            ErrFp = stdout;
  139.            break;
  140.  
  141.         case 'h':
  142.         case 'H':
  143.            Hush = 1;
  144.            break;
  145.  
  146. #ifdef HAVE_QUEUED
  147.         case 'z':
  148.         case 'Z':
  149.            DontFork = 1;
  150.            PARSENUM(Daemon, arg);
  151.            if (Daemon<5) Daemon=5;
  152.            else if (Daemon>60) Daemon=60;
  153.            break;
  154.  
  155.         case 'a':
  156.         case 'A':
  157.            DontIssueAts = 1;
  158.            break;
  159.  
  160.         case 'q':
  161.         case 'Q':
  162.            DontQueue = 1;
  163.            break;
  164.  
  165.         case 'f':
  166.         case 'F':
  167.            DontFork = 1;
  168.            break;
  169. #endif
  170.         case 'c':
  171.         case 'C':
  172.            DoCalendar = 1;
  173.            if (*arg == '+') {
  174.               arg++;
  175.               PARSENUM(CalWeeks, arg);
  176.           if (!CalWeeks) CalWeeks = 1;
  177.                } else {
  178.                  PARSENUM(CalMonths, arg);
  179.               if (!CalMonths) CalMonths = 1;
  180.                }
  181.            break;
  182.  
  183.         case 's':
  184.         case 'S':
  185.            DoSimpleCalendar = 1;
  186.            PARSENUM(CalMonths, arg);
  187.            if (!CalMonths) CalMonths = 1;
  188.            break;
  189.  
  190.         case 'w':
  191.         case 'W':
  192.            PARSENUM(CalWidth, arg);
  193.            if (CalWidth < 80) CalWidth = 80;
  194.            break;
  195.  
  196.         case 'd':
  197.         case 'D':
  198.            while (*arg) {
  199.               switch(*arg++) {
  200.              case 'e': case 'E': DebugFlag |= DB_ECHO_LINE; break;
  201.              case 'x': case 'X': DebugFlag |= DB_PRTEXPR;   break;
  202.              case 't': case 'T': DebugFlag |= DB_PRTTRIG;   break;
  203.              case 'v': case 'V': DebugFlag |= DB_DUMP_VARS; break;
  204.              case 'l': case 'L': DebugFlag |= DB_PRTLINE;   break;
  205.              default:
  206.                 fprintf(ErrFp, "Unknown debug flag '%c'\n", *(arg-1));
  207.           }
  208.                }
  209.            break;
  210.  
  211.         case 'v':
  212.         case 'V':
  213.            DebugFlag |= DB_PRTLINE;
  214.            ShowAllErrors = 1;
  215.            break;
  216.  
  217.         case 'b':
  218.         case 'B':
  219.            PARSENUM(ScFormat, arg);
  220.            if (ScFormat<0 || ScFormat>2) ScFormat=SC_AMPM;
  221.            break;
  222.  
  223.         case 'x':
  224.         case 'X':
  225.            PARSENUM(MaxSatIter, arg);
  226.            if (MaxSatIter < 10) MaxSatIter=10;
  227.            break;
  228.  
  229.         default:
  230.            fprintf(ErrFp, "Unknown option '%c'\n", *(arg-1));
  231.      }
  232.  
  233.       }
  234.    }
  235.  
  236.  
  237.    /* Get the filename. */
  238.    if (i >= argc) {
  239.       Usage();
  240.       exit(1);
  241.    }
  242.    InitialFile = argv[i];
  243.    i++;
  244.  
  245.    /* Get the date, if any */
  246.    if (i < argc) {
  247.       while (i < argc) {
  248.          arg = argv[i++];
  249.          FindToken(arg, &tok);
  250.      switch (tok.type) {
  251.         case T_Month:
  252.            if (m != NO_MON) Usage();
  253.            else m = tok.val;
  254.            break;
  255.  
  256.         case T_Day:
  257.            if (d != NO_DAY) Usage();
  258.            else d = tok.val;
  259.            break;
  260.  
  261.         case T_Year:
  262.            if (y != NO_YR) Usage();
  263.            else y = tok.val;
  264.            break;
  265.  
  266.         default: Usage();
  267.          }
  268.       }
  269.  
  270. /* Must supply date in the form:  day, mon, yr OR mon, yr */
  271.       if (m == NO_MON || y == NO_YR) Usage();
  272.       if (d == NO_DAY) d=1;
  273.       if (d > DaysInMonth(m, y)) {
  274.          fprintf(ErrFp, BadDate);
  275.      Usage();
  276.       }
  277.       JulianToday = Julian(y, m, d);
  278.       if (JulianToday == -1) {
  279.          fprintf(ErrFp, BadDate);
  280.      Usage();
  281.       }
  282.       CurYear = y;
  283.       CurMon = m;
  284.       CurDay = d;
  285.       if (JulianToday != RealToday) IgnoreOnce = 1;
  286.    }
  287. /* Set JulFirst */
  288.    JulFirst = Julian(CurYear, 0, 1);
  289.    FirstYear = CurYear;
  290. }
  291.  
  292. /***************************************************************/
  293. /*                                                             */
  294. /*  Usage                                                      */
  295. /*                                                             */
  296. /*  Print the usage info.                                      */
  297. /*                                                             */
  298. /***************************************************************/
  299. #ifdef HAVE_PROTOS
  300. PUBLIC void Usage(void)
  301. #else
  302. void Usage()
  303. #endif
  304. {
  305.    fprintf(ErrFp, "\nREMIND %s Copyright 1992 by David F. Skoll\n\n", VERSION);
  306.    fprintf(ErrFp, "Usage: remind [options] filename [date]\n\n");
  307.    fprintf(ErrFp, "Options:\n");
  308.    fprintf(ErrFp, " -n     Output next occurrence of reminders in simple format\n");
  309.    fprintf(ErrFp, " -r     Disable RUN directives\n");
  310.    fprintf(ErrFp, " -c[n]  Produce a calendar for n (default 1) months\n");
  311.    fprintf(ErrFp, " -c+[n] Produce a calendar for n (default 1) weeks\n");
  312.    fprintf(ErrFp, " -w[n]  Specify width (default 80) of calendar output\n");
  313.    fprintf(ErrFp, " -s[n]  Produce 'simple calendar' for n (1) months\n");
  314.    fprintf(ErrFp, " -v     Verbose mode\n");
  315.    fprintf(ErrFp, " -o     Ignore ONCE directives\n");
  316.    fprintf(ErrFp, " -t     Trigger all future reminders regardless of delta\n");
  317.    fprintf(ErrFp, " -h     'Hush' mode - be very quiet\n");
  318. #ifdef HAVE_QUEUED
  319.    fprintf(ErrFp, " -a     Don't trigger timed reminders immediately - just queue them\n");
  320.    fprintf(ErrFp, " -q     Don't queue timed reminders\n");
  321.    fprintf(ErrFp, " -f     Trigger timed reminders by staying in foreground\n");
  322.    fprintf(ErrFp, " -z[n]  Enter daemon mode, waking every n (5) minutes.\n");
  323. #endif
  324.    fprintf(ErrFp, " -d...  Debug: e=echo x=expr-eval t=trig v=dumpvars l=showline\n");
  325.    fprintf(ErrFp, " -e     Divert messages normally sent to stderr to stdout\n");
  326.    fprintf(ErrFp, " -b[n]  Time format for cal: 0=am/pm, 1=24hr, 2=none\n");
  327.    fprintf(ErrFp, " -x[n]  Iteration limit for SATISFY clause (def=150)\n");
  328.    exit(1);
  329. }
  330.